# some utility functions/macros implemented in cmake script language
# these functionalities may be implemented by cmake itself, so we 
# here use the util_ prefix

if(NOT __CMKUTILS_DEFINED)
set(__CMKUTILS_DEFINED TRUE)
#-------------------------------------------------------------------------

# remove_definitions and add_definitions is cmake builtin

# util_remove_cflags([debug]|[optimized] flag1 flag2...)
# remove can use regular expression supported by cmake
macro(util_remove_cflags a1)
  if( ${a1} STREQUAL debug )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST  CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG )
  elseif( ${a1} STREQUAL optimized )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST  CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
                          CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  else() 
    set(__ARG_LIST ${ARGV})
    set(__LOCAL_CFG_LIST
                          CMAKE_C_FLAGS 
                          CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
                          CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
                          CMAKE_CXX_FLAGS 
                          CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
                          CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  endif()
  foreach(locv ${__LOCAL_CFG_LIST})
    foreach(lfg ${__ARG_LIST})
      string(REGEX REPLACE ${lfg} " " ${locv} "${${locv}}")
    endforeach()
  endforeach()
endmacro()

# util_add_cflags([debug]|[optimized] flag1 flag2...)
# there is no flags conflict analysis
macro(util_add_cflags a1)
  if( ${a1} STREQUAL debug )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST  CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG )
  elseif( ${a1} STREQUAL optimized )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST  CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
                          CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  else() 
    set(__ARG_LIST ${ARGV})
    set(__LOCAL_CFG_LIST  CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
  endif()
  foreach(locv ${__LOCAL_CFG_LIST})
    foreach(lfg ${__ARG_LIST})
      set(${locv} "${${locv}} ${lfg}")
    endforeach()
  endforeach()
endmacro()

# util_remove_ldflags([debug]|[optimized] flag1 flag2...)
# remove can use regular expression supported by cmake
macro(util_remove_ldflags a1)
  if( ${a1} STREQUAL debug )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST CMAKE_EXE_LINKER_FLAGS_DEBUG CMAKE_SHARED_LINKER_FLAGS_DEBUG CMAKE_MODULE_LINKER_FLAGS_DEBUG)
  elseif( ${a1} STREQUAL optimized )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST  CMAKE_EXE_LINKER_FLAGS_RELEASE 
                          CMAKE_EXE_LINKER_FLAGS_MINSIZEREL
                          CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO      
                          
                          CMAKE_SHARED_LINKER_FLAGS_RELEASE
                          CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL
                          CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO

                          CMAKE_MODULE_LINKER_FLAGS_RELEASE 
                          CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL
                          CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO                          
                          )
  else() 
    set(__ARG_LIST ${ARGV})
    set(__LOCAL_CFG_LIST
                          CMAKE_EXE_LINKER_FLAGS
                          CMAKE_EXE_LINKER_FLAGS_RELEASE 
                          CMAKE_EXE_LINKER_FLAGS_MINSIZEREL
                          CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO      
                          
                          CMAKE_SHARED_LINKER_FLAGS
                          CMAKE_SHARED_LINKER_FLAGS_RELEASE
                          CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL
                          CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO

                          CMAKE_MODULE_LINKER_FLAGS
                          CMAKE_MODULE_LINKER_FLAGS_RELEASE 
                          CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL
                          CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO   
                          )
  endif()
  foreach(locv ${__LOCAL_CFG_LIST})
    foreach(lfg ${__ARG_LIST})
      string(REGEX REPLACE ${lfg} " " ${locv} "${${locv}}")
    endforeach()
  endforeach()
endmacro()

# util_add_ldflags([debug]|[optimized] flag1 flag2...)
# there is no flags conflict analysis
macro(util_add_ldflags a1)
  if( ${a1} STREQUAL debug )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST  CMAKE_EXE_LINKER_FLAGS_DEBUG CMAKE_SHARED_LINKER_FLAGS_DEBUG CMAKE_MODULE_LINKER_FLAGS_DEBUG)
  elseif( ${a1} STREQUAL optimized )
    set(__ARG_LIST ${ARGN})
    set(__LOCAL_CFG_LIST  CMAKE_EXE_LINKER_FLAGS_RELEASE 
                          CMAKE_EXE_LINKER_FLAGS_MINSIZEREL
                          CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO      
                          
                          CMAKE_SHARED_LINKER_FLAGS_RELEASE
                          CMAKE_SHARED_LINKER_FLAGS_MINSIZEREL
                          CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO

                          CMAKE_MODULE_LINKER_FLAGS_RELEASE 
                          CMAKE_MODULE_LINKER_FLAGS_MINSIZEREL
                          CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO                          
                          )
  else() 
    set(__ARG_LIST ${ARGV})
    set(__LOCAL_CFG_LIST CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS)
  endif()
  foreach(locv ${__LOCAL_CFG_LIST})
    foreach(lfg ${__ARG_LIST})
      set(${locv} "${${locv}} ${lfg}")
    endforeach()
  endforeach()
endmacro()

#-------------------------------------------------------------------------
endif()
